<script type="text/javascript">

    function moveTaskItem(item) {
        var to_element = 'completed';
        if (Element.up($(item)).id == 'completed') {
            to_element = 'open';
            $$('#' + item + ' input')[0].checked = false;
        }
        else {
            $$('#' + item + ' input')[0].checked = true;
            $$('#' + item + ' .edit')[0].hide();
        }
        $(to_element).insert($(item));
        $(item).removeClassName('busy');
    }

    function showBusy(item) {
        $(item).addClassName('busy');
    }

</script>
<script type="text/javascript" src="/javascripts/calendar_date_select/calendar_date_select.js"></script>
<link type="text/css" rel="stylesheet" href="/stylesheets/calendar_date_select/plain.css"></link>
<div id="task">
  <div class="header">
    <h2>Tasks</h2>
  </div>
  <div class="nav">
  </div>
  <div class="main tasks" id="tasks">
    <h2>Open</h2>
    <ul class="open" id="open">
      <% assign_to_array = [['no one', '0']] | @tasks_view[:people].map { |person| [person.name, person.id] } %>
      <% @tasks_view[:tasks].each do |task|
          if task.status == TASK_STATUS_OPEN %>
              <li class="task" id="item_<%= task.id %>">
                <div class="view">
                    <span class="status">
                      <%= check_box_tag("status_#{task.id}", TASK_STATUS_OPEN, false,
                                        {:onchange => remote_function(:url     => {:action => :toggle_status},
                                                                      :with    => "'id=#{task.id}'",
                                                                      :loading => "showBusy('item_#{task.id}')",
                                                                      :success => "moveTaskItem('item_#{task.id}')")
                                        }) %>
                    </span>
                  <span class="name"> <%= task.name %> </span>
                  <% unless task.project.nil? %>
                      <span class="project">(<%= task.project.name %>)</span>
                  <% end %>
                  <span class="userdueby">
                        <%= task.user.name %> <%= task.due_date.to_date if !task.due_date.nil? %>
                    </span>
                  <%= link_to_function("edit", :class => 'edit', :id => "showedit_#{task.id}") do |page|
                      page.visual_effect(:toggle_appear, "edit_#{task.id}")
                      page << "this.innerHTML = (this.innerHTML == 'edit' ? 'close' : 'edit')"
                  end
                  %>
                </div>
                <div class="edit" id="edit_<%= task.id %>" style="display:none;">
                  <%= form_for(task, :remote => true, :url => {:controller => "task", :action => "edit"}) do |f| %>
                      <%= f.hidden_field(:id) %>
                      <p>
                        <%= f.text_field(:name) %>

                        <%= f.fields_for(task.project) do |project_form|%>
                            <%= project_form.text_field(:name) %>
                        <% end %>
                      </p>

                      <p>
                        <%= f.calendar_date_select(:due_date, :time => false) %>
                      </p>

                      <p>
                        <%= f.text_field(:time_spent) %>
                      </p>

                      <p>
                        <%= f.select(:user, options_for_select(assign_to_array, task.user.id)) %>
                      </p>

                      <p>
                        <%= f.submit %>
                      </p>
                  <% end %>
                </div>
                  <%= javascript_event_tag("edit_task_#{task.id}", "ajax:complete") do |page|
                        page.visual_effect(:toggle_appear, "edit_#{task.id}")
                        page["showedit_#{task.id}"].replace_html("edit")

                      end
                  %>
              </li>
          <% end
             end %>
    </ul>

    <h2>Completed</h2>
    <ul class="completed" id="completed">

      <% @tasks_view[:tasks].each do |task|
          if task.status == TASK_STATUS_COMPLETED %>
              <li class="task" id="item_<%= task.id %>">
                <div class="view">
                    <span class="status">
                      <%= check_box_tag("status_#{task.id}", TASK_STATUS_COMPLETED, true,
                                        {:onchange => remote_function(:url     => {:action => :toggle_status},
                                                                      :with    => "'id=#{task.id}'",
                                                                      :loading => "showBusy('item_#{task.id}')",
                                                                      :success => "moveTaskItem('item_#{task.id}')")
                                        }) %>
                    </span>
                  <span class="name"> <%= task.name %> </span>
                  <% unless task.project.nil? %>
                      <span class="project">(<%= task.project.name %>)</span>
                  <% end %>
                  <span class="userdueby">
                        <%= task.user.name %> <%= task.due_date.to_date if !task.due_date.nil? %>
                    </span>
                </div>
              </li>
          <% end
             end %>
    </ul>
  </div>
  <div class="sidebar">
    <%= form_tag do %>
        <ul>
          <li>
            <% projects_array = [['all projects', nil], ['just tasks', 0]] | @tasks_view[:projects].map { |project| [project.name, project.id] } %>
            <span class="label"> </span> <%= select_tag(:project,
                                                        options_for_select(projects_array, @tasks_view[:project])) %>
          </li>
          <% unless @tasks_view[:people].nil? %>
              <li>
                <% people_array = [['everyone\'s tasks', 0]] | @tasks_view[:people].map { |person| [person.name, person.id] } %>
                <span class="label"></span> <%= select_tag(:person,
                                                           options_for_select(people_array, @tasks_view[:person])) %>
              </li>
          <% end %>
          <li>
            <span class="label">from: </span> <%= calendar_date_select_tag("start_date", @tasks_view[:start_date], :time => false) %>
          </li>
          <li>
            <span class="label">to: </span> <%= calendar_date_select_tag("end_date", @tasks_view[:end_date], :time => false) %>
          </li>
          <li>
            <input type="submit" value="filter"/>
          </li>
        </ul>
    <% end %>
  </div>
</div>
